﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Super.DomainService;
using Super.Entities;
using Super.Infrastructure;
using Super.Mvc;

namespace Super.Web.Areas.Admin.Controllers
{
    public class LackManageController : BaseController
    {
        //
        // GET: /Admin/LackManage/

        public ActionResult Index()
        {
            var adp = new DbContextAdapter();
            var lackService = new LackService(adp);
            var page = Query.Params("pageNum");
            if (string.IsNullOrEmpty(page))
            {
                page = "1";
            }
            var where = PredicateBuilder.True<Lack>();
            where = where.And(a => a.IsDel == false);

            #region 查询

            var keyWord = Query.Post("keyword");

            var startTime = Query.Post("startTime") == "" ? DateTime.MinValue : Convert.ToDateTime(Query.Post("startTime"));
            var endTime = Query.Post("endTime") == "" ? DateTime.MinValue : Convert.ToDateTime(Query.Post("endTime"));

            ViewBag.keyword = keyWord;

            ViewBag.starTime = startTime;
            ViewBag.endTime = endTime;

            if (!string.IsNullOrEmpty(keyWord))
            {
                where = where.And(
                        a =>
                        a.Name.Contains(keyWord) ||
                        a.Summary.Contains(keyWord));

            }

            if (startTime != DateTime.MinValue)
            {
                where = where.And(a => a.CreateTime >= startTime);
            }
            if (endTime != DateTime.MinValue)
            {
                endTime = endTime.AddDays(1);
                where = where.And(a => a.CreateTime <= endTime);
            }
            #endregion

            var lack = lackService.Repository.Find(where).OrderByDescending(a => a.ID).Page(page.ToInt(), PageSize);
            ViewBag.Records = lack.ItemCount;//记录数
            //ViewBag.User= user.ItemCollection.ToList();
            BindPage(lack.ItemCount, page.ToInt(), PageSize);
            return View(lack.ItemCollection.ToList());
        }

        public ActionResult Edit()
        {
            var id = Query.Get("id").ToInt(0);
            if (id > 0)
            {
                var adp = new DbContextAdapter();
                var lackService = new LackService(adp);
                var lackModel = lackService.Repository.Find(a => a.ID == id).FirstOrDefault();
                return View(lackModel);
            }
            return View();
        }

        [HttpPost]
        public JsonResult Edit(Lack model)
        {
            if(model.ID<=0)
            {
                model.CreateTime = DateTime.Now;
            }
            var adp = new DbContextAdapter();
            var lackService = new LackService(adp);
            lackService.Repository.InsertOrUpdate(model);
            adp.SaveChanges();
            return JsonBack.Tip(adp.ErrorMessage, "lackmanageIndex");

        }

        public JsonResult Delete()
        {
            var id = Query.Get("id").ToInt(0);
            var adp = new DbContextAdapter();
            var lackService = new LackService(adp);
            var lackModel = lackService.Repository.Find(a => a.ID == id).FirstOrDefault();
            if (lackModel != null)
            {
                lackService.Repository.Delete(lackModel);
            }
            adp.SaveChanges();
            return JsonBack.Tip(adp.ErrorMessage, "lackmanageIndex");
        }
    }
}
